﻿
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Globalization;
using System.Windows.Data;

namespace caedisi.Converters
{
    /// <summary>
    /// Visibility converter needed by xaml
    /// </summary>
    public class VisibilityConverter : IValueConverter
    {
        public object Convert(
            object value,
            Type targetType,
            object parameter,
            CultureInfo culture)
        {
            bool visibility = (bool)value;
            return visibility ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(
            object value,
            Type targetType,
            object parameter,
            CultureInfo culture)
        {
            Visibility visibility = (Visibility)value;
            return (visibility == Visibility.Visible);
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class BoolToValueConverter<T> : IValueConverter
    {
        public T FalseValue { get; set; }
        public T TrueValue { get; set; }

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return FalseValue;
            else
                return (bool)value ? TrueValue : FalseValue;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value != null ? value.Equals(TrueValue) : false;
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public class BoolToVisibilityConverter : BoolToValueConverter<Visibility> { }

    /// <summary>
    /// 
    /// </summary>
    public class CultureToVisibilityConverter : IValueConverter
    {
        public object Convert(
               object value,
               Type targetType,
               object parameter,
               CultureInfo culture)
        {
            bool isVisible = true;

            CultureInfo currentCulture = (CultureInfo) value;
            string currentParameter = (string) parameter;

            if (currentCulture != null && currentParameter!=null)
            {
                isVisible = (string.Compare(currentCulture.TwoLetterISOLanguageName, currentParameter, StringComparison.CurrentCultureIgnoreCase) ==0);
            }  
          
            return isVisible ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(
            object value,
            Type targetType,
            object parameter,
            CultureInfo culture)
        {
            //not needed
            throw new NotImplementedException();
        }
    }

    /// <summary>
    /// A converter which convert a empty string into a visibility property
    /// if the string is empty, the visibility is collapsed
    /// </summary>
    public class EmptyStringVisibilityConverter : IValueConverter
    {
        public Visibility FalseValue { get; set; }
        public Visibility TrueValue { get; set; }

        public object Convert(
            object value,
            Type targetType,
            object parameter,
            CultureInfo culture)
        {
            string valueAsString = (string)value;
            return string.IsNullOrEmpty(valueAsString) ? TrueValue: FalseValue;
        }

        public object ConvertBack(
            object value,
            Type targetType,
            object parameter,
            CultureInfo culture)
        {
            //not needed
            throw new NotImplementedException();
        }
    }

}

